using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;

public class VirtualBottonHandler : MonoBehaviour
{
    public Material m_VirtualButtonDefault;
    public Material m_VirtualButtonPressed;
    public float m_ButtonreleaseTimeDelay;
    VirtualButtonBehaviour[] virtualButtonBehaviours;
    public AudioSource[] audios;
    // Start is called before the first frame update
    private void Awake()
    {
        virtualButtonBehaviours = GetComponentsInChildren<VirtualButtonBehaviour>();
        for(int i=0;i<virtualButtonBehaviours.Length;++i)
        {
            virtualButtonBehaviours[i].RegisterOnButtonPressed(OnButtonPressed);
            virtualButtonBehaviours[i].RegisterOnButtonReleased(OnButtonReleased);
        }
    }
    void Destory()
    {
        virtualButtonBehaviours = GetComponentsInChildren<VirtualButtonBehaviour>();

        for(int i=0;i<virtualButtonBehaviours.Length;++i)
        {
            virtualButtonBehaviours[i].UnregisterOnButtonPressed(OnButtonPressed);
            virtualButtonBehaviours[i].UnregisterOnButtonReleased(OnButtonReleased);
        }
    }

    public void OnButtonPressed(VirtualButtonBehaviour vb)
    {
        Debug.Log("OnButtonPressed:" + vb.VirtualButtonName);

        SetVirtualButtonMaterial(m_VirtualButtonPressed);
        StopAllCoroutines();
        BroadcastMessage("HandleVirtualButtonPressed", SendMessageOptions.DontRequireReceiver);
        switch(vb.VirtualButtonName)
        {
            case "1":
                audios[0].Play();
                break;
            case "2":
                audios[1].Play();
                break;
            case "3":
                audios[2].Play();
                break;
            case "4":
                audios[3].Play();
                break;
            case "5":
                audios[4].Play();
                break;
            case "6":
                audios[5].Play();
                break;
            case "7":
                audios[6].Play();
                break;
            default:
                break;
        }
    }

    public void OnButtonReleased(VirtualButtonBehaviour vb)
    {
        Debug.Log("OnButtonReleased:" + vb.VirtualButtonName);
        SetVirtualButtonMaterial(m_VirtualButtonDefault);
        StartCoroutine(DelayOnButtonReleasedEvent(m_ButtonreleaseTimeDelay, vb.VirtualButtonName));
    }
    void SetVirtualButtonMaterial(Material material)
    {
        for(int i=0;i<virtualButtonBehaviours.Length;++i)
        {
            if(material!=null)
            {
                virtualButtonBehaviours[i].GetComponent<MeshRenderer>().sharedMaterial = material;
            }
        }
    }
    IEnumerator DelayOnButtonReleasedEvent(float waitTime,string buttonName)
    {
        yield return new WaitForSeconds(waitTime);
        BroadcastMessage("HandleVirtualButtonReleased", SendMessageOptions.DontRequireReceiver);
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}
